package com.ono;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

public class Upload {

	public static void main(String[] args) {
		postFile("http://734f47fe.ngrok.io/upload", new ProgressListener() {
			
			@Override
			public void onProgress(long totalBytes, long writedBytes, boolean done) {
				System.out.println((int)(writedBytes*100.0/totalBytes)+"%");
				
			}
		}, new File("D:\\360安全浏览器下载\\ngrok-stable-windows-amd64.zip"));
		
		System.out.println("上传文件");
	}
	public static void postFile(String url, final ProgressListener listener, File... files) {

		MultipartBody.Builder builder = new MultipartBody.Builder();
		builder.setType(MultipartBody.FORM);
		builder.addFormDataPart("file", files[0].getName(),
				RequestBody.create(MediaType.parse("application/octet-stream"), files[0]));

		MultipartBody multipartBody = builder.build();

		Request request = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody, listener)).build();
		OKHttpUtil.CLIENT.newCall(request).enqueue(new Callback() {
			
			@Override
			public void onResponse(Call call, Response response) throws IOException {
				response.close();
			}
			
			@Override
			public void onFailure(Call call, IOException e) {
				
				
			}
		});
	}

	public static class ProgressRequestBody extends RequestBody {
		public static final int UPDATE = 0x01;
		private RequestBody requestBody;
		private ProgressListener mListener;
		private BufferedSink bufferedSink;

		public ProgressRequestBody(RequestBody body, ProgressListener listener) {
			requestBody = body;
			mListener = listener;

		}

		@Override
		public MediaType contentType() {
			return requestBody.contentType();
		}

		@Override
		public long contentLength() throws IOException {
			return requestBody.contentLength();
		}

		@Override
		public void writeTo(BufferedSink sink) throws IOException {

			if (bufferedSink == null) {
				bufferedSink = Okio.buffer(sink(sink));
			}
			// 写入
			requestBody.writeTo(bufferedSink);
			// 刷新
			bufferedSink.flush();
		}

		private Sink sink(BufferedSink sink) {

			return new ForwardingSink(sink) {
				long bytesWritten = 0L;
				long contentLength = 0L;

				@Override
				public void write(Buffer source, long byteCount) throws IOException {
					super.write(source, byteCount);
					if (contentLength == 0) {
						contentLength = contentLength();
					}
					bytesWritten += byteCount;
					mListener.onProgress(contentLength, bytesWritten, bytesWritten == contentLength);
				}
			};
		}
	}

	interface ProgressListener {
		void onProgress(long totalBytes, long writedBytes, boolean done);
	}

}
